package com.easywan.web.controller.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.service.AdminMenueService;
import com.easywan.service.BaseService;
import com.easywan.util.StringsUtils;
import com.easywan.util.Utils;
import com.easywan.vo.Pages;
import com.easywan.vo.base.BaseModel;

@Controller
@RequestMapping("admin")
public class IndexControllerAdmin {
	private static Logger logger = Logger.getLogger(IndexControllerAdmin.class);
	@Autowired
	ApplicationContext applicationContext;
	@Autowired
	AdminMenueService adminMenuService;
	
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Pages page){
		JSPModelAndView jspView = new JSPModelAndView("admin/index");
		if(true){
			/*jspView.addObject("pname", request.getParameter("pname"));
			jspView.addObject("name", request.getParameter("name"));*/
			jspView.addObject("data",adminMenuService.selectAll());
			return jspView;
		}
		
		
		String model = request.getParameter("model");//判断是那个模块
		String action = request.getParameter("action");//判断是那个功能
		String file = request.getParameter("file");//判断是那个功能
		if(file == null){
			file = model;
		}
	//	String mothod = request.getParameter("method");//判断是什么事件
		jspView.addObject("model", model);
		if(null != model){ 
			BaseService baseService = (BaseService) applicationContext.getBean(model + "ServiceImpl");
			//baseService.setMapper((IBaseDao) applicationContext.getBean(model+"Mapper"));
			try {
				Class<?> cls = Class.forName("com.easywan.vo."+StringsUtils.firestToUpper(model));
				
				if(action != null)
				switch(action){
				default:
				case "index":
					
					BaseModel baseModel = Utils.loadFiled(request, cls);//(BaseModel) cls.newInstance();
					if(page == null) page = new Pages();
					baseModel.setPage(page);
					jspView.addObject("data",baseService.list(baseModel));
					jspView.addObject("page",page);
					jspView.setViewName("admin/"+file+"/"+action+".jsp");
					return jspView;
				case "del":
					Integer id = Integer.valueOf(request.getParameter("id"));
					int result = baseService.deleteById(id);
					Utils.sendMsg(response, result + "");
					return null;
				case "modify":
					Integer id1 = Integer.valueOf(request.getParameter("id"));
					jspView.addObject("data",baseService.findById(id1));
					jspView.setViewName("admin/"+file+"/"+action+".jsp");
					return jspView;
				case "modify_chk":
					Integer id2 = Integer.valueOf(request.getParameter("id"));
					baseService.modify(Utils.loadFiled(request,cls).setId(id2));
					Utils.sendMsg(response, 1 + "");
					return null;
				case "add":
					jspView.setViewName("admin/"+file+"/"+action+".jsp");
					return jspView;
				}
					
			} catch (Exception e) {
				//System.out.println("没有找到对应Model" + "com.easywan.vo."+model);
				e.printStackTrace();
				logger.error(e.getMessage());
				return jspView;
			} 
		}
		
		return jspView;
	}
	@RequestMapping("welcome.html")
	public ModelAndView welcome(){
		return new JSPModelAndView("admin/welcome/welcome");
	}
	@RequestMapping("{name}.html")
	public ModelAndView html(@PathVariable("name") String html){
		return new JSPModelAndView("admin/" + html);
	}
}
